#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2017/11/6 0006 11:03
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : myfunction.py
import re                               #正则
from collections import Iterable        #判断是否可迭代
import xml.sax  # xml解析


# 自定义函数
class ComRe(object):
    #判断输入的类型
    def input_type(a):
        atype=re.search('[a-zA-Z]+?',a)
        if atype:
            return str
        else:
            atype=re.search('^(\-|\+)?\d+\.\d+$',a)
            if atype:
                return float
            else:
                atype=re.search('\W+',a)
                if atype:
                    return str
                else:
                    return int
    #组合成字典
    def to_dict(a,b,c=0):
        if isinstance(a,(list,tuple)) and isinstance(b,(list,tuple)):
            i=len(a)
            j=len(b)
            d={}
            if i>j:
                for m in range(i):
                    if m>j-1:
                        d[a[m]]=c
                    else:
                        d[a[m]]=b[m]
            else:
                for m in range(i):
                    d[a[m]]=b[m]
            return d
        else:
            return "Wrong type of input"
    #返回输入list/tuple/dict/set的最大最小值返回tuple
    def get_num(a):
        if isinstance(a, Iterable):
            small = 0
            big = 0
            for k, v in enumerate(a):
                if k == 0:
                    small = a[k]
                    big = a[k]
                else:
                    if small > a[k]:
                        small = a[k]
                    if big < a[k]:
                        big = a[k]
            num = [small, big]
            print(tuple(num))
    #时间戳时间转化// 貌似不能输出汉语版格式化文件
    def my_time(mtime=0,type=1):
        import time
        if mtime>0:
            a=mtime
            a=time.localtime(int(a))
        if type==0:
            new_time=int(time.time())
        elif type==1:
            if mtime>5:
                new_time = time.strftime("%Y/%m/%d %H:%M:%S",a)
            else:
                new_time = int(time.time() * (10 ** type))
        elif type==2:
            if mtime>5:
                new_time = time.strftime("%Y/%m/%d %H:%M:%S",a)
            else:
                new_time = int(time.time() * (10 ** type))
        else:
            new_time = int(time.time()*(10**type))
        return new_time
    #字符串转int map/reduce方式
    def str2int(s):
        from functools import reduce
        def calc(x, y):
            return x * 10 + y
        def act(s):
            num_list = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
            return num_list[s]
        return reduce(calc, map(act, s))
    #求字符串的乘积
    def prod(num):
        from functools import reduce
        def calc(x, y):
            return x * y
        def get_list(num):
            num_list = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
            return num_list[num]
        return reduce(calc, map(get_list, num))
    #字符串转float   #分割字符串，计算之前将字符反转一下
    def str2float(s):
        from functools import reduce
        s=s.split('.')
        def f1(x,y):
            return x*10+y
        def f2(x,y):
            return x/10+y
        def get_list(num):
            num_list = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
            return num_list[num]
        return reduce(f1,map(get_list,s[0]))+reduce(f2,map(get_list,s[1][::-1]))/10


# xml格式字符串转化为字典
class XMLHandler(xml.sax.handler.ContentHandler):
    def __init__(self):
        self.buffer = ""
        self.mapping = {}
    def startElement(self, name, attributes):
        self.buffer = ""
    def characters(self, data):
        self.buffer += data
    def endElement(self, name):
        self.mapping[name] = self.buffer
    def getDict(self):
        return self.mapping